<?php
session_start();
require_once( 'student_db.php' );
 
$m_db = new M_Student();
$user_id = $_SESSION['userid'];
 
if (isset($_POST['m'])  &&  $_POST['m']!=""){

    if ($_POST['m']== 'load'){
	  $cid = $_POST['cid'];
	  $r = $_POST['r'];
	  $s = $_POST['s'];
	  $n = $_POST['n'];
	  $co = $_POST['co'];
	  
      $result=$m_db->loadStudentMove_Exit($co,$n,$cid,$r,$s);
      $rows=$m_db->getRowCount();
      $i=1;
      $html="";
      if ($rows>0){
           $data = array();
			$today = date("Y-m-d");	
			$year = date("Y");
            while($row = $result->FetchRow()){

               $data[] = array(0,$row['student_id'],$row['status'],$row['class_id'],$row['code'], $row['title_name'] ." ". $row['thai_name'] . " " .$row['thai_surname']  
			   ,$row['class_name'], $row['room'] , $row['status_name'],$row['remark'] ) ;
			   
            }
        }else{
			$data = array();
        }
    
        $arr = array('aaData'=>$data);
        
        echo json_encode($arr);
     
    }else if ($_POST['m']=='save'){
		$type = $_POST['type'];
		$id = $_POST['id'];
		
		$ar = $_POST['info'];
		
		if ($ar['birthdate']!=""){
			list($d, $m, $y)= explode("/",$ar['birthdate']); 
			$ar['birthdate'] = $y."/".$m."/".$d;
		}
		$ar['map'] = isset($_SESSION['Photo'])?$_SESSION['Photo']:'' ;
		
		$ar['updatedate'] =date( "Y/m/d H:i:s" );
		$ar['updateby'] = $user_id;
		
		if ($type=="o")
			$m_db->updateOldStudent($id,$ar);
		else
			$m_db->updateNewStudent($id,$ar);
			
	
    }else if ($_POST['m']=='add'){
        $ar = array();
		$ar = $_POST['info'];
	 
		list($d, $m, $y)= explode("/",$ar['birthdate']); 
		list($d1,$m1,$y1) = explode("/",$ar['registerdate']);
		
		$ar['map'] = isset($_SESSION['Photo'])?$_SESSION['Photo']:'' ;
		$ar['birthdate'] = $y."/".$m."/".$d;
		$ar['registerdate']= $y1."/".$m1."/".$d1;
		
		$ar['createdate'] =date( "Y/m/d H:i:s" );
		$ar['createby'] = $user_id;
		$ar['updatedate'] =date( "Y/m/d H:i:s" );
		$ar['updateby'] = $user_id;
		 
		 $m_db->insertNewStudent($ar);
	 
		
	    $_SESSION['Photo']="";
		echo 1;
		
    }  
}else{
	if (isset($_GET['m']) && $_GET['m'] !="" ){
		if ($_GET['m'] == 'class'){
			echo json_encode( $m_db->getClassLevel());
		}else if ($_GET['m']=='term'){
			echo  json_encode( $m_db->getTermPayment());
		}else if ($_GET['m']=='assets'){
			echo  json_encode($m_db->getAssetsPayment());
		}
	} 
   
}

function DATEDIFF($date1, $date2){
	
}
 
?>